package com.hptpd.questionnairesurvey.controller.admin;

import com.google.common.collect.Maps;
import com.hptpd.questionnairesurvey.component.Result;
import com.hptpd.questionnairesurvey.controller.base.BaseController;
import com.hptpd.questionnairesurvey.service.IQuestionnaireService;
import com.hptpd.questionnairesurvey.vo.excel.ImportQuestionnaireVo;
import com.hptpd.questionnairesurvey.vo.QuestionnaireVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019-07-16 09:15
 * \*
 * \* Description:
 * \
 *
 * @author waiterlong
 */
@RestController
@RequestMapping("/api/admin")
public class QuestionnaireController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(QuestionnaireController.class);

    @Resource(name = "iQuestionnaireService")
    private IQuestionnaireService iQuestionnaireService;

    /**
     * 分页列出问卷
     *
     * @param name  {String}
     * @param tag   {String}
     * @param page  {Integer}
     * @param limit {Integer}
     * @return {Result}
     */
    @RequestMapping(value = "/questionnaire/list", method = RequestMethod.GET)
    public Result listQuestionnairesByPage(
            @RequestParam(defaultValue = "") String name,
            @RequestParam(defaultValue = "") String tag,
            @RequestParam(defaultValue = "") String state,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        Pageable pageable = PageRequest.of(page - 1, limit);
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("name", name);
        paramMap.put("tag", tag);
        paramMap.put("state", state);
        return iQuestionnaireService.listQuestionnairesByPage(paramMap, pageable);
    }

    /**
     * 删除问卷
     *
     * @param questionnaireVo {QuestionnaireVo}
     * @return {Result}
     */
    @RequestMapping(value = "/questionnaire/delete", method = RequestMethod.POST)
    public Result deleteQuestionnaire(
            @RequestBody QuestionnaireVo questionnaireVo) {
        return iQuestionnaireService.deleteQuestionnaire(questionnaireVo);
    }

    /**
     * 发布问卷
     *
     * @param questionnaireVo {QuestionnaireVo}
     * @return {Result}
     */
    @RequestMapping(value = "/questionnaire/issue", method = RequestMethod.POST)
    public Result issueQuestionnaire(
            @RequestBody QuestionnaireVo questionnaireVo) {
        return iQuestionnaireService.issueQuestionnaire(questionnaireVo);
    }

    @RequestMapping(value = "/questionnaire/new", method = RequestMethod.POST)
    public Result newQuestionnaire(@RequestBody QuestionnaireVo questionnaireVo) {
        Result result = iQuestionnaireService.saveQuestionnaire(questionnaireVo);
        logger.info(result.toString());
        return result;
    }

    @RequestMapping(value = "/questionnaire/copy", method = RequestMethod.POST)
    public Result copyQuestionnaire(@RequestBody QuestionnaireVo questionnaireVo) {
        return iQuestionnaireService.copyQuestionnaire(questionnaireVo);
    }

    @RequestMapping(value = "/questionnaire/get/{id}", method = RequestMethod.GET)
    public Result getQuestionnaire(@PathVariable String id){
        Result result = iQuestionnaireService.getQuestionnaireById(id);
        logger.info(result.toString());
        return result;
    }

    @RequestMapping(value = "/questionnaire/delete/dimension/{id}", method = RequestMethod.DELETE)
    public Result deleteDimensionality(@PathVariable String id) {
        if (id.equals("")) {
            return Result.badArgument();
        }
        iQuestionnaireService.deleteDimension(id);
        return Result.ok();
    }

    @RequestMapping(value = "/questionnaire/delete/single/{id}", method = RequestMethod.DELETE)
    public Result deleteSingleChoiceQuestion(@PathVariable String id) {
        if (id.equals("")) {
            return Result.badArgument();
        }
        iQuestionnaireService.deleteSingleQuestion(id);
        return Result.ok();
    }

    @RequestMapping(value = "/questionnaire/delete/choice/{id}", method = RequestMethod.DELETE)
    public Result deleteChoice(@PathVariable String id) {
        if (id.equals("")) {
            return Result.badArgument();
        }
        iQuestionnaireService.deleteChoice(id);
        return Result.ok();
    }

    @RequestMapping(value = "/questionnaire/delete/fill/{id}", method = RequestMethod.DELETE)
    public Result deleteFillUpQuestion(@PathVariable String id) {
        if (id.equals("")) {
            return Result.badArgument();
        }
        iQuestionnaireService.deleteFillQuestion(id);
        return Result.ok();
    }

    @RequestMapping(value = "/questionnaire/upload", method = RequestMethod.POST)
    public Result importQuestionnaire(MultipartFile multipartFile) {
        return iQuestionnaireService.importQuestionnaire(multipartFile);
    }
}
